/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.web;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ems.entity.EmsArea;
import com.thinkgem.jeesite.modules.ems.entity.EmsDept;
import com.thinkgem.jeesite.modules.ems.service.EmsAreaService;
import com.thinkgem.jeesite.modules.ems.service.EmsDeptService;

/**
 * 部门Controller
 * @author zad
 * @version 2018-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ems/emsDept")
public class EmsDeptController extends BaseController {

	@Autowired
	private EmsDeptService emsDeptService;
	
	@Autowired
	private EmsAreaService emsAreaService;
	
	@ModelAttribute
	public EmsDept get(@RequestParam(required=false) String id) {
		EmsDept entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = emsDeptService.get(id);
		}
		if (entity == null){
			entity = new EmsDept();
		}
		return entity;
	}
	
	@RequiresPermissions("ems:emsDept:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmsDept emsDept, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EmsDept> page = emsDeptService.findPage(new Page<EmsDept>(request, response), emsDept); 
		model.addAttribute("page", page);
		
		// add by zad 2018-01-25 区域查询
		List<EmsArea> areas = this.emsAreaService.findList(null);
		model.addAttribute("areas", areas);
		
		return "modules/ems/emsDeptList";
	}

	@RequiresPermissions("ems:emsDept:view")
	@RequestMapping(value = "form")
	public String form(EmsDept emsDept, Model model) {
		
		// add by zad 2018-01-25 区域查询
		List<EmsArea> areas = this.emsAreaService.findList(null);
		model.addAttribute("areas", areas);
		
		model.addAttribute("emsDept", emsDept);
		return "modules/ems/emsDeptForm";
	}

	@RequiresPermissions("ems:emsDept:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request, HttpServletResponse response, EmsDept emsDept, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsDept)){
			return form(emsDept, model);
		}
		
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 
		 String dateStr = DateUtils.getDate("yyyy/MM/dd");
		/** 构建文件保存的目录* */
	      String logoPathDir = "userfiles/1/images/cms/dept/logo/" + dateStr + "/";
	      /** 得到文件保存目录的真实路径* */
	      /*String logoRealPathDir = request.getSession().getServletContext()
	              .getRealPath(logoPathDir);*/
	      String logoRealPathDir = Global.getUserfilesBaseDir()+logoPathDir;
	      
	      /** 根据真实路径创建目录* */
	      File logoSaveFile = new File(logoRealPathDir);
	      if (!logoSaveFile.exists())
	      {
	          logoSaveFile.mkdirs();
	      }
	      /** 页面控件的文件流* */
	      MultipartFile multipartFile = multipartRequest.getFile("file");
	      if(multipartFile.getSize() > 0){
	    	  /** 获取文件的后缀* */
	          //String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
	                  
	          /** 使用UUID生成文件名称* */
	          //String logImageName = UUID.randomUUID().toString() + suffix;// 构建文件名称
	          String logImageName = multipartFile.getOriginalFilename();// 构建文件名称
	                  
	          /** 拼成完整的文件保存路径加文件* */
	          String fileName = logoRealPathDir + File.separator + logImageName;
	          File file = new File(fileName);
	          try {
	              multipartFile.transferTo(file);
	          } catch (IllegalStateException e) {
	              e.printStackTrace();
	          } catch (IOException e) {
	              e.printStackTrace();
	          }
	          emsDept.setLogo(request.getContextPath() + "/"+logoPathDir+logImageName);
	      }
		
		emsDeptService.save(emsDept);
		addMessage(redirectAttributes, "保存部门成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsDept/?repage";
	}
	
	@RequiresPermissions("ems:emsDept:edit")
	@RequestMapping(value = "delete")
	public String delete(EmsDept emsDept, RedirectAttributes redirectAttributes) {
		emsDeptService.delete(emsDept);
		addMessage(redirectAttributes, "删除部门成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsDept/?repage";
	}

}